//
//  MasterViewController.m
//  Moosnick Museum Application
//
//  Created by Justin Blackburn, Mike McNary, Dan Bastin, Will Martin
//	Transylvania University 2010
//

#import "MasterViewController.h"
#import "DetailViewController.h"

@implementation MasterViewController

@synthesize detailViewController;
@synthesize categories;
@synthesize categoryTitles;
@synthesize url;
@synthesize webpageContent;
@synthesize error;

#pragma mark -
#pragma mark Rotation support

// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}


#pragma mark -
#pragma mark Size for popover
// The size the view should be when presented in a popover.
- (CGSize)contentSizeForViewInPopoverView {
    return CGSizeMake(200.0, 280.0);
}


#pragma mark -
#pragma mark View lifecycle


 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	url = [NSURL URLWithString:@"http://www.cs.transy.edu/mmcnary/catlist.cgi"];
	webpageContent = [NSString stringWithContentsOfURL:url
										   encoding:NSASCIIStringEncoding
											  error:&error];

	categoryTitles = [webpageContent componentsSeparatedByString:@"|"];
	
	[categoryTitles removeObjectAtIndex:0];	
	[categoryTitles removeLastObject];
	NSLog(@"categories: %@", categoryTitles);
	
	self.categories = categoryTitles;
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [categories count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	static NSString *CellIdentifier = @"CellIdentifier";
	
	// Dequeue or create a cell of the appropriate type.
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    // Get the object to display and set the value in the cell.
    cell.textLabel.text = [self.categories objectAtIndex:indexPath.row];
    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    /*
     When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
     */
    detailViewController.detailItem = [self.categories objectAtIndex: indexPath.row];
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    
    [detailViewController release];
    [super dealloc];
}

@end
